/*
 * Copyright (c) 2011 Obix Labs Limited
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:
 * 
 * 		Redistribution of source code must retain the above 
 * 		copyright notice, this list of conditions and the 
 * 		following disclaimer.
 *
 * 		Redistribution in binary form must reproduce the 
 * 		above copyright notice, this list of conditions 
 * 		and the following disclaimer in the documentation 
 * 		and/or other materials provided with the distribution.
 * 
 * 	THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF 
 * 	ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS 
 * 	AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
 * 	FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, 
 * 	ARE HEREBY EXCLUDED. OBIX LABS LIMITED ("Obix Labs") AND ITS 
 * 	LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE 
 * 	AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR 
 * 	ITS DERIVATIVES. IN NO EVENT WILL Obix Labs OR ITS LICENSORS BE 
 * 	LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 
 * 	INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE 
 * 	DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF 
 * 	LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS 
 * 	SOFTWARE, EVEN IF Obix Labs HAS BEEN ADVISED OF THE POSSIBILITY OF 
 * 	SUCH DAMAGES.
 */
package com.obixlabs.commons.zip;

import java.util.Arrays;

import com.obixlabs.commons.util.SimpleHashCodeCalculator;

/**
 * <p>
 * Represents a single or unary element extracted from a
 * compressed archive. This would typically be a single file. 
 * </p>
 */
public class CompressedData extends CompressedItem
{
	private static final long serialVersionUID = 6983647173603119328L;

	/**
	 * <p>
	 * The zip element's data.
	 * </p>
	 */
	private byte[] data;
	
	/**
	 * <p>
	 * Constructs an instance with the given name
	 * and byte data.
	 * </p>
	 * 
	 * @param name	The name of the item.
	 * @param data	The data to be encapsulated by the 
	 * instance.
	 */
	public CompressedData(String name, byte[] data)
	{
		super(name);
		this.data = data;
	}

	/**
	 * <p>
	 * Returns the data encapsulated by the item.
	 * </p>
	 * 
	 * @return	The data encapsulated by the item.
	 */
	public byte[] getData() {return data;}
	
	@Override
	public int hashCode()
	{		
		return SimpleHashCodeCalculator.calculateFromFields(getName(), data);
	}

	@Override
	public boolean equals(Object obj)
	{
		boolean result = false;
		
		if (this == obj) result = true;
		else if (obj instanceof CompressedData)
		{
			CompressedData other = (CompressedData) obj;
			if (Arrays.equals(data, other.data) && 
						super.equals(other)) 
				result = true;
		}		
		return result;
	}

	@Override
	public String toString()
	{
		return "CompressedData [name='" + getName() +"',     " +
						" data=" + Arrays.toString(data) + "]";
	}
	
	
}
